package com.example.springstudyvue.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springstudyvue.entity.News;
import com.example.springstudyvue.mapper.NewsMapper;
import com.example.springstudyvue.result.Result;
import com.example.springstudyvue.service.NewsService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author linKe
 * @since 2021-11-26
 */
@Api(value = "新闻处理接口",tags="newsApi")
@RestController
@RequestMapping("/news")
public class NewsController {

    @Resource
    private NewsService newsService;
    @Resource
    private NewsMapper newsMapper;




    @ApiOperationSupport(author = "linKe 的新增书籍接口")
    @ApiOperation(value="根据书籍信息新增",notes="返回操作结果")
    @PostMapping()
    public Result<Boolean> addNews(@RequestBody News news) {
        return Result.success(newsService.addNews(news));
    }

    @ApiOperation(value="根据书籍更新数据",notes="返回操作结果")
    @PutMapping()
    public Result<Boolean> updateNews(@RequestBody News news) {
        return Result.success(newsService.updateNews(news));
    }

    @ApiOperation(value="根据书籍ID删除",notes="返回操作结果")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteNews(@PathVariable("id") Long newsId) {
        return Result.success(newsService.deleteNews(newsId));
    }






    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(name = "search",defaultValue = "") String search) {

        LambdaQueryWrapper<News> wrapper = Wrappers.<News>lambdaQuery();
        Page<News> newsPage = new Page<>(pageNum, pageSize);

        //huTool的工具包 判断search不是空的前提下
        if (StrUtil.isNotBlank(search)) {
            //News::getNickName 调用news的nickName字段   News ::getAccount 或者账号名
            wrapper.like(News::getAuthor, search);
        }
        Page<News> newsPages = newsMapper.selectPage(newsPage, wrapper);

        return new Result<>("200","ok",newsPages);
    }


}
